package cn.howxu.yichen_ovo.common.item;

import cn.howxu.yichen_ovo.init.ModItems;
import net.minecraft.ChatFormatting;
import net.minecraft.client.resources.language.I18n;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.animal.Pig;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.*;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @description: TODO
 * @author: HowXu
 * @date: 2025/10/1 08:56
 */
public class Spawn extends Item {
    public Spawn() {
        super(new Properties().rarity(Rarity.UNCOMMON));
    }

    @Override
    public InteractionResult useOn(UseOnContext pContext) {
        if (!(pContext.getLevel().isClientSide())){
                Entity entity = pContext.getLevel().random.nextInt(2) == 0 ? EntityType.PIG.create(pContext.getLevel()) : EntityType.WITCH.create(pContext.getLevel());
                
                entity.setPos(pContext.getClickedPos().above().getCenter());
                entity.setCustomName(Component.translatable("entity.yichenovo.yichen.name"));
                entity.setCustomNameVisible(true);
                pContext.getLevel().addFreshEntity(entity);
        }
        // 消耗式
        var stack = pContext.getPlayer().getItemInHand(pContext.getHand()); // 这里不可能是null
        stack.shrink(1);
        return InteractionResult.CONSUME;
    }
    

    @Override
    public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Component> pTooltipComponents, TooltipFlag pIsAdvanced) {
        super.appendHoverText(pStack, pLevel, pTooltipComponents, pIsAdvanced);
        pTooltipComponents.add(Component.translatable("tip.yichenoco.spawn").withStyle(ChatFormatting.AQUA));
    }
}
